import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
// 引入框架和首页
import Layout from '@/layout/layout.vue'
import Login from '@/views/login'

//隐藏原地跳转错误
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 配置路由 和页面一一对应
const routes = []

// 路由一分为三
//静态路由：  所有能访问的页面
export const constantRoutes = [
  // 默认首页
  {
    path: '/',
    redirect: '/home',
  },
  // 登录页
  {
    path: '/login',
    component: Login,
  },
  {
    path: '/home',
    component: Layout, //框架
    meta: {path: '@/views/home/index.vue', title: '后台首页', icon: 'home'},
    children: [
      {
        path: '',
        component: () => import('@/views/home/index.vue'),
      },
    ],
  },
]

//动态路由
export const asnycRoutes = [
  // 订单管理
  {
    path: '/order',
    component: Layout, //框架
    meta: {
      path: '/order/index',
      title: '订单管理',
      roles: ['super'],
      icon: 'order',
    },
    children: [
      {
        path: '',
        component: () => import('@/views/order/index.vue'),
      },
      {
        path: '/order/order-detail',
        component: () => import('@/views/order/order-detail'),
      },
      {
        path: '/order/order-edit/:id',
        component: () => import('@/views/order/order-edit'),
      },
    ],
  },

  // 商品列表
  {
    path: '/goods',
    component: Layout, //框架
    meta: {path: '/goods', title: '商品列表', icon: 'goods'},
    redirect: '/goods/goods-list',
    children: [
      {
        meta: {path: '/goods/goods-list', title: '商品列表'},
        path: '/goods/goods-list', //不写斜杠相当于/goods/goods-list
        component: () => import('@/views/goods/goods-list'),
      },
      {
        meta: {path: '/goods/goods-add', title: '商品添加', roles: ['super']},
        path: '/goods/goods-add',
        component: () => import('@/views/goods/goods-add'),
      },
      {
        meta: {path: '/goods/goods-cate', title: '商品分类'},
        path: '/goods/goods-cate',
        component: () => import('@/views/goods/goods-cate'),
      },
    ],
  },
  // 商店
  {
    path: '/shop',
    component: Layout, //框架
    meta: {path: '/shop', title: '订单管理', roles: ['super'], icon: 'shop'},
    children: [
      {
        path: '',
        component: () => import('@/views/shop'),
      },
    ],
  },
  // 账号列表
  {
    path: '/account',
    component: Layout,
    meta: {path: '/account', title: '账号管理', icon: 'account'},
    redirect: '/account/account-list',
    children: [
      {
        meta: {
          path: '/account/account-list',
          title: '账号列表',
          roles: ['super'],
        },
        path: '/account/account-list',
        component: () => import('@/views/account/account-list'),
      },
      {
        meta: {
          path: '/account/account-add',
          title: '账号添加',
          roles: ['super'],
        },
        path: '/account/account-add',
        component: () => import('@/views/account/account-add'),
      },
      {
        meta: {path: '/account/password-modify', title: '修改密码'},
        path: '/account/password-modify',
        component: () => import('@/views/account/password-modify'),
      },
      {
        meta: {path: '/account/account-person', title: '个人中心'},
        path: '/account/account-person',
        component: () => import('@/views/account/account-person'),
      },
    ],
  },
  // 销售统计
  {
    path: '/total',
    component: Layout,
    meta: {path: '/total', title: '销售统计', roles: ['super'], icon: 'total'},
    redirect: '/total/total-goods',
    children: [
      {
        meta: {path: '/total/total-order', title: '商品统计'},
        path: '/total/total-order',
        component: () => import('@/views/total/total-order'),
      },
      {
        meta: {path: '/total/total-goods', title: '订单统计'},
        path: '/total/total-goods',
        component: () => import('@/views/total/total-goods'),
      },
    ],
  },
]

//错误路由 404相关
export const errorRoutes = [
  //错误路由
  {
    path: '*',
    redirect: '/404',
  },
  {
    path: '/404',
    component: () => import('@/views/404/error404.vue'),
  },
]

const router = new VueRouter({
  //初始化加载静态路由
  routes: constantRoutes,
})

export default router
